package com.sright.bcard.content.biz.service;

import com.aliyuncs.exceptions.ClientException;
import com.sright.bcard.content.biz.manager.MessageManager;
import com.sright.bcard.common.service.share.dto.page.PageParamRequest;
import com.sright.bcard.common.service.share.dto.pager.Pager;
import com.sright.bcard.common.service.share.utils.DataConvertUtils;
import com.sright.bcard.content.dal.dao.MessageDAO;
import com.sright.bcard.common.service.share.dto.content.MessageDTO;
import com.sright.bcard.content.dal.entity.MessageDO;
import com.sright.bcard.content.share.service.MessageService;
import com.sright.common.response.SrightResponse;
import com.taobao.api.ApiException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageManager messageManager;
    @Autowired
    MessageDAO messageDAO;

    @Override
    public SrightResponse referrer(Long memberId) {
        return SrightResponse.defaultSuccess(messageManager.referrer(memberId));
    }

    @Override
    public SrightResponse addMessage(MessageDTO messageDTO) throws ApiException, NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, ClientException, ParseException {
        return SrightResponse.defaultSuccess(messageManager.addMessage(messageDTO));
    }

    @Override
    public MessageDTO getDetailById(Long messageId) {
        MessageDO messageDO =  messageDAO.selectById(messageId);
        MessageDTO messageDTO = new MessageDTO();
        DataConvertUtils.convertDO2DTO(messageDO, messageDTO);
        return messageDTO;
    }

    @Override
    public Pager<MessageDTO> searchByKeyword(String keyword, Integer status, Integer isOpened, Integer pageIndex, Integer pageSize) {
        Integer count = messageDAO.count(keyword, status, isOpened);
        PageParamRequest request = new PageParamRequest();
        Integer offset = (pageIndex - 1) * pageSize;
        request.setOffset(offset);
        request.setLimit(pageSize);
        List<MessageDO> doList = messageDAO.pageList(keyword, status, isOpened, request);
        List<MessageDTO> dtoList = new ArrayList<>();
        DataConvertUtils.convertDOList2DTOList(doList, dtoList, MessageDTO.class);
        Pager<MessageDTO> pager = new Pager<>(pageIndex, pageSize, count, dtoList);
        return pager;
    }

    @Override
    public Boolean updateById(Long id, Integer status, String content) {
        MessageDO messageDO = messageDAO.selectById(id);
        messageDO.setStatus(status);
        messageDO.setContent(content);
        return messageDAO.updateSelective(messageDO);
    }

    @Override
    public Boolean updateOpenStatus(Long id, boolean isOpening) {
        MessageDO messageDO = messageDAO.selectById(id);
        messageDO.setIsOpening(isOpening ? 1 : 0);
        return messageDAO.updateSelective(messageDO);
    }
}
